import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class PokedexBuilder {
	static final int POKEMON_COUNT = 7;
	Document doc;

	public PokedexBuilder() throws Exception {
		String baseURL="http://serebii.net/pokedex-dp/";

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		doc = db.newDocument();
		Element root = doc.createElement("pokemon-db");
		doc.appendChild(root);

		for(int i=100; i<100+POKEMON_COUNT; i++){
			System.out.println(i);
			String pageurl="rawfiles/dppage"+String.format("%03d",i)+".html";


			String s = convertStreamToString(new FileInputStream(pageurl));
			root.appendChild(process(root, s));

			//outfile.println(String.format("%03d", i)+result);
		}	

		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");

		//initialize StreamResult with File object to save to file
		StreamResult result = new StreamResult(new StringWriter());
		DOMSource source = new DOMSource(doc);
		transformer.transform(source, result);
		String xmlString = result.getWriter().toString();
		
		BufferedWriter out = new BufferedWriter(new FileWriter("out.xml"));
		out.write(xmlString);
		out.close();

	}

	/* TODO:
	 * type(s)
	 * abilitie(s)
	 * ability description
	 * 
	 */
	public String createPattern() {
		String pattern = "";

		pattern += ".*<title>Serebii.*#\\d\\d\\d (.*?)</title>";
		pattern += ".*<td><b>National</b>: </td><td>(.*?)</td></tr><tr><td><b>Johto</b>: </td><td>(.*?)</td></tr>";	
		//pattern += ".*<tr> <td>Male <font color=.*?</font>:</td><td>(.*?)%</td></tr><tr><td>Female";
		//pattern += ".*<b>Ability</b>: <a href=.*?><b>(.*?)</b></a> </td>";

		pattern += ".*";
		return pattern;
	}

	public Element process(Element root, String page) throws IOException, ParserConfigurationException, TransformerException, TransformerConfigurationException {
		Element e = doc.createElement("pokemon");

		String pat = createPattern();

		Pattern p = Pattern.compile(pat, Pattern.DOTALL);
		Matcher m = p.matcher(page);

		m.matches();

		e.setAttribute("id1", m.group(2));
		e.setAttribute("id2", m.group(3));
		e.setAttribute("name", m.group(1));
		//e.setAttribute("male", m.group(4));
		//e.setAttribute("ability", m.group(5));

		/*
		Element root1 = doc.createElement("pokemon2");
		e.appendChild(root1);
		 */
		//Node n = new Node
		//e.appendChild("stats1");

		return e;
	}

	public static void main(String[] args) throws Exception{
		new PokedexBuilder();
	}

	public String convertStreamToString(InputStream is) throws IOException {
		/*
		 * To convert the InputStream to String we use the BufferedReader.readLine()
		 * method. We iterate until the BufferedReader return null which means
		 * there's no more data to read. Each line will appended to a StringBuilder
		 * and returned as String.
		 */
		if (is != null) {
			StringBuilder sb = new StringBuilder();
			String line;

			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				while ((line = reader.readLine()) != null) {
					sb.append(line).append("\n");
				}
			} finally {
				is.close();
			}
			return sb.toString();
		} else {        
			return "";
		}
	}

}
